/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.sql.Timestamp;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DbConfig
/*     */ {
/*     */   private static final String CN_NAME = "name";
/*     */   private static final String CN_VALUE = "value";
/*     */   private static final String CN_DESCRIPTION = "value";
/*     */   private static final String CN_MODIFIED = "modified";
/*     */   private static final int CI_NAME = 1;
/*     */   private static final int CI_VALUE = 2;
/*     */   private static final int CI_DESCRIPTION = 3;
/*     */   private static final int CI_MODIFIED = 4;
/*     */   private String mDbName;
/*     */   private String mDbValue;
/*     */   private String mDbDescription;
/*     */   private Timestamp mDbModified;
/*     */   
/*     */   public static DbConfig set(DbPool.DbConnection conn, String name, String value)
/*     */     throws ServiceException
/*     */   {
/*  60 */     Timestamp modified = new Timestamp(System.currentTimeMillis());
/*     */     
/*  62 */     PreparedStatement stmt = null;
/*     */     try
/*     */     {
/*  65 */       stmt = conn.prepareStatement("UPDATE config SET value = ?, modified = ? WHERE name = ?");
/*  66 */       stmt.setString(1, value);
/*  67 */       stmt.setTimestamp(2, modified);
/*  68 */       stmt.setString(3, name);
/*  69 */       int numRows = stmt.executeUpdate();
/*     */       
/*     */ 
/*  72 */       if (numRows == 0) {
/*  73 */         stmt = conn.prepareStatement("INSERT INTO config(name, value, modified) VALUES (?, ?, ?)");
/*  74 */         stmt.setString(1, name);
/*  75 */         stmt.setString(2, value);
/*  76 */         stmt.setTimestamp(3, modified);
/*  77 */         stmt.executeUpdate();
/*     */       }
/*     */     } catch (SQLException e) {
/*  80 */       throw ServiceException.FAILURE("writing config entry: " + name, e);
/*     */     } finally {
/*  82 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */     
/*  85 */     DbConfig c = new DbConfig();
/*  86 */     c.mDbName = name;
/*  87 */     c.mDbValue = value;
/*  88 */     c.mDbModified = modified;
/*  89 */     return c;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static boolean delete(DbPool.DbConnection conn, String name)
/*     */     throws ServiceException
/*     */   {
/* 101 */     PreparedStatement stmt = null;
/*     */     try {
/* 103 */       stmt = conn.prepareStatement("DELETE FROM config WHERE name = ?");
/* 104 */       stmt.setString(1, name);
/* 105 */       int num = stmt.executeUpdate();
/* 106 */       return num == 1;
/*     */     } catch (SQLException e) {
/* 108 */       throw ServiceException.FAILURE("deleting config entry: " + name, e);
/*     */     } finally {
/* 110 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static DbConfig get(DbPool.DbConnection conn, String name)
/*     */     throws ServiceException
/*     */   {
/* 123 */     PreparedStatement stmt = null;
/* 124 */     ResultSet rs = null;
/* 125 */     DbConfig result = null;
/*     */     try {
/* 127 */       stmt = conn.prepareStatement("SELECT * FROM config WHERE name = ?");
/* 128 */       stmt.setString(1, name);
/* 129 */       rs = stmt.executeQuery();
/* 130 */       if (rs.next())
/* 131 */         return constructConfig(rs);
/*     */     } catch (SQLException e) {
/* 133 */       throw ServiceException.FAILURE("getting config entry: " + name, e);
/*     */     } finally {
/* 135 */       DbPool.closeResults(rs);
/* 136 */       DbPool.closeStatement(stmt);
/*     */     }
/* 138 */     return result;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Map<String, DbConfig> getAll(DbPool.DbConnection conn, Timestamp ts)
/*     */     throws ServiceException
/*     */   {
/* 151 */     PreparedStatement stmt = null;
/* 152 */     ResultSet rs = null;
/* 153 */     HashMap<String, DbConfig> result = new HashMap();
/*     */     try {
/* 155 */       if (ts == null) {
/* 156 */         stmt = conn.prepareStatement("SELECT * FROM config");
/*     */       } else {
/* 158 */         stmt = conn.prepareStatement("SELECT * FROM config WHERE modified >= ?");
/* 159 */         stmt.setTimestamp(1, ts);
/*     */       }
/* 161 */       rs = stmt.executeQuery();
/* 162 */       while (rs.next()) {
/* 163 */         DbConfig c = constructConfig(rs);
/* 164 */         result.put(c.mDbName, c);
/*     */       }
/*     */     } catch (SQLException e) {
/* 167 */       throw ServiceException.FAILURE("getting all config entries", e);
/*     */     } finally {
/* 169 */       DbPool.closeResults(rs);
/* 170 */       DbPool.closeStatement(stmt);
/*     */     }
/* 172 */     return result;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private static DbConfig constructConfig(ResultSet rs)
/*     */     throws SQLException
/*     */   {
/* 180 */     DbConfig c = new DbConfig();
/* 181 */     c.mDbName = rs.getString(1);
/* 182 */     c.mDbValue = rs.getString(2);
/* 183 */     c.mDbDescription = rs.getString(3);
/* 184 */     c.mDbModified = rs.getTimestamp(4);
/* 185 */     return c;
/*     */   }
/*     */   
/*     */   public String getName() {
/* 189 */     return this.mDbName;
/*     */   }
/*     */   
/*     */   public String getValue() {
/* 193 */     return this.mDbValue;
/*     */   }
/*     */   
/*     */   public Timestamp getModified() {
/* 197 */     return this.mDbModified;
/*     */   }
/*     */   
/*     */   public String toString() {
/* 201 */     StringBuffer sb = new StringBuffer();
/* 202 */     sb.append("config: {");
/* 203 */     sb.append("name").append(": ").append(this.mDbName).append(", ");
/* 204 */     sb.append("value").append(": ").append(this.mDbValue).append(", ");
/* 205 */     sb.append("value").append(": ").append(this.mDbDescription).append(", ");
/* 206 */     sb.append("modified").append(": ").append(this.mDbModified);
/* 207 */     sb.append("}");
/* 208 */     return sb.toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/DbConfig.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */